#include <iostream>
#include"head.h"

void read(Node*phead, int a)
{
	FILE*fp;
	char fname[]="Employee_file.dat";
	fp=fopen(fname,"rb");//二进制读的方式打开磁盘文件
					
	for(int i  =  0; i < a; i++)
	{
		Employee* E = (Employee*)malloc(sizeof(Employee));//堆中申请区域

		int number = fread(E, sizeof(Employee), 1, fp);//二进制读，从磁盘中读取数据
		if (number == 0)//若无法读出数据，则跳出循环
		{
			break;
		}
						
		Node *pnew  =  (Node*)malloc(sizeof(Node));

		pnew->data = E;/*通过函数指针对数据域赋值*/
		pnew->next  =  NULL;

		pnew->next = phead->next;//头插法建立链表
		phead->next = pnew;
	}
	
	return ;

}
	
void write(Node*phead)
{
	FILE*fp;//定义文件指针
	char fname[]="Employee_file.dat";
	fp=fopen(fname,"wb");//二进制写的方式打开磁盘文件

	Node* p = phead->next;
	while(p!= NULL)
	{
		Employee* E = (Employee*)p->data;//数据域赋值
		
		int number = fwrite(E,sizeof(Employee),1,fp);//二进制写，将链表中的数据写入磁盘中
		if (number == 0)//若无法写出数据，则跳出循环
		{
			break;
		} 

		p = p->next;//指针后移
	}
	
	fclose(fp);
}
